<template>
	<view class="add-card-container">
		<view class="warning-tip">仅支持私人银行卡，不支持公司银行卡！</view>

		<view class="form-section">
			<view class="form-item">
				<text class="label">开户银行</text>
				<picker @change="handleBankChange" :range="bankList" range-key="bankName" :value="bankIndex">
					<view class="picker-content">
						<text v-if="selectedBank">{{selectedBank.bankName}}</text>
						<text v-else class="placeholder">请选择开户银行</text>
						<text class="arrow">›</text>
					</view>
				</picker>
			</view>

			<view class="form-item">
				<text class="label">开户人姓名</text>
				<input type="text" v-model="formData.accountName" placeholder="请输入开户人姓名" class="input" />
			</view>

			<view class="form-item">
				<text class="label">银行卡号</text>
				<input type="number" v-model="formData.cardNumber" placeholder="请输入银行卡号" class="input" />
			</view>
		</view>

		<button class="save-btn" @tap="handleSave">保存</button>
	</view>
</template>

<script>
	import {
		getBankList,
		addBankCard
	} from '@/api/wallet/bankCard.js'

	export default {
		data() {
			return {
				formData: {
					accountName: '',
					cardNumber: '',
					bankCode: '',
					bankName: ''
				},
				bankList: [],
				bankIndex: -1,
				selectedBank: null
			}
		},
		onLoad() {
			this.getBankList()
		},
		methods: {
			async getBankList() {
				try {
					const res = await getBankList()
					if (res.code === 200) {
						this.bankList = res.data
					}
				} catch (error) {
					console.error('获取银行列表失败:', error)
				}
			},
			handleBankChange(e) {
				const index = e.detail.value
				this.bankIndex = index
				this.selectedBank = this.bankList[index]
				this.formData.bankCode = this.selectedBank.bankCode
				this.formData.bankName = this.selectedBank.bankName
			},
			async handleSave() {
				if (!this.formData.bankCode || !this.formData.accountName || !this.formData.cardNumber) {
					uni.showToast({
						title: '请填写完整信息',
						icon: 'none'
					})
					return
				}

				try {
					const res = await addBankCard(this.formData)
					if (res.code === 200) {
						uni.showToast({
							title: '添加成功',
							icon: 'success'
						})
						setTimeout(() => {
							const pages = getCurrentPages()
							const prevPage = pages[pages.length - 2]
							if (prevPage && prevPage.$vm.getMyCards) {
								prevPage.$vm.getMyCards()
							}
							uni.navigateBack()
						}, 1500)
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '添加失败',
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add-card-container {
		min-height: 100vh;
		background: #f5f5f5;
		padding: 20rpx;

		.warning-tip {
			color: #ff6b35;
			font-size: 28rpx;
			text-align: center;
			padding: 20rpx;
		}

		.form-section {
			background: #fff;
			border-radius: 12rpx;
			padding: 30rpx;

			.form-item {
				margin-bottom: 30rpx;

				&:last-child {
					margin-bottom: 0;
				}

				.label {
					font-size: 28rpx;
					color: #666;
					margin-bottom: 20rpx;
					display: block;
				}

				.input {
					width: 100%;
					height: 88rpx;
					background: #f5f5f5;
					border-radius: 8rpx;
					padding: 0 20rpx;
					font-size: 28rpx;
				}
			}
		}

		.save-btn {
			width: 100%;
			height: 88rpx;
			line-height: 88rpx;
			background: #4CAF50;
			color: #fff;
			border-radius: 44rpx;
			font-size: 32rpx;
			margin-top: 40rpx;
		}

		.picker-content {
			width: 100%;
			height: 88rpx;
			background: #f5f5f5;
			border-radius: 8rpx;
			padding: 0 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			
			.placeholder {
				color: #999;
			}
			
			.arrow {
				font-size: 32rpx;
				color: #999;
				transform: rotate(90deg);
			}
		}
	}
</style>